﻿#include "BrowserTableHandlerJunctionAreaSEA.h"
#include "OperatorsInterface/OperAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAAbstractJunction.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEAJunctionArea.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysGeoProp.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>

namespace GUI
{
    void BrowserTableHandlerJunctionAreaSEA::updateTable()
    {
        if (_tableWidget == nullptr || _junMgr == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        QList<Acoustics::FITKAbstractJunction*> list = _junMgr->getJointsByType(
            Acoustics::FITKAbstractJunction::JunctionType::SEAAreaJunction);
        for (int i = 0; i < list.size(); ++i)
        {
            // 转换为连接对象
            Acoustics::FITKSEAJunctionArea* junction = dynamic_cast<Acoustics::FITKSEAJunctionArea*>(list.at(i));
            if (junction == nullptr) continue;
            //记录最大层数
            int layers = junction->getSubSysIds().size();
            if (layers > _maxlayers)
                _maxlayers = layers;
        }
        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled") << tr("Area[m2]") << tr("Leaks");
        for (int i = 0; i < _maxlayers; ++i)
        {
            QString Layer = QString("Connection-%1").arg(i + 1);
            headerLabels << Layer;
        }
        headerLabels << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerJunctionAreaSEA::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_seaMgr == nullptr || _junMgr == nullptr)return;

        QList<Acoustics::FITKAbstractJunction*> list = _junMgr->getJointsByType(
            Acoustics::FITKAbstractJunction::JunctionType::SEAAreaJunction);
        //物理属性数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            // 转换为连接对象
            Acoustics::FITKSEAJunctionArea* junction = dynamic_cast<Acoustics::FITKSEAJunctionArea*>(list.at(i));
            if (junction == nullptr) continue;

            //数据ID
            const int id = junction->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(junction->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(junction->getEnabledJunction());

            connect(box, &QCheckBox::stateChanged, this, [junction, id](int state) {
                if (state == Qt::Checked)  junction->setEnabledJunction(true);
                else if (state == Qt::Unchecked)  junction->setEnabledJunction(false);
                EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
                if (operGraph)operGraph->updateGraph(id);
            });
            _tableWidget->setCellWidget(i, 1, box);

            int shellID = junction->getConnectionShellID();
            auto shellobj = _seaMgr->getDataByID(shellID);
            if (!shellobj)continue;
            double area = shellobj->getGeoProp()->getGeoProp(Acoustics::FITKSEASubSysGeoProp::Area);
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(area), id));

            QList<Acoustics::FITKSEAJunAreaLeakStr> define = junction->getLeaks();
            int total = define.size();
            int isLeak = 0;
            for (Acoustics::FITKSEAJunAreaLeakStr str : define){
                if (str._isEnabled)  ++isLeak;
            }
            if(total==0)
                _tableWidget->setItem(i, 3, new QTableWidgetItem(QString("0"), id));
            else
                _tableWidget->setItem(i, 3, new QTableWidgetItem(QString("%1/%2").arg(isLeak).arg(total), id));

            QList<int> subs = junction->getSubSysIds();
            for (int j = 0; j < subs.size(); ++j)
            {
                Acoustics::FITKAbstractSEASubSys* data = dynamic_cast<Acoustics::FITKAbstractSEASubSys*>(_seaMgr->getDataByID(subs[j]));
                if (!data)continue;
                _tableWidget->setItem(i, 3 + j + 1, new QTableWidgetItem(data->getDataObjectName(), id));
            }
            //_tableWidget->setItem(i, 1 + _maxlayers + 1, new QTableWidgetItem("描述", id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractJunction * BrowserTableHandlerJunctionAreaSEA::getJunctionOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractJunction>("OperJunctionAreaSEA");
    }

    bool BrowserTableHandlerJunctionAreaSEA::enableAttriSet()
    {
        //再说
        return false;
    }
}